package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class ReturnBook implements IOPeration{
    @Override
    public void work(BookList bookList){
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入你要归还图书的书号");
        String id0 = scan.next();
        //用于记录归还图书的索引位置
        int num = -1;
        for (int i = 0; i < bookList.books.size(); i++) {
            //检查是否有该书号的书
            Book Book = bookList.books.get(i);
            if (Book.id.equals(id0)) {
                num = i;
                System.out.println(Book);
            }
        }
        if(num == -1){
            System.out.println("未找到该书号的书");
            return;
        }
        Book Book = bookList.books.get(num);
        if(Book.name.contains("(已借)")) {
            //为书名后删除标记
            //(已借)长度为4,删除只需要取(原长度-4)的字段修改书名就好了
            String name0 = (String) Book.name.subSequence(0, Book.name.length() - 4);
            Book.setName(name0);
            //增加相应的库存
            Book.setNum(Book.getNum() + 1);
            bookList.books.set(num, Book);
            System.out.println("归还成功");
        }else {
            System.out.println("该书未被借阅,归还失败");
        }
    }
}
